package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.netmodle.resp.PutForwardTypeEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PutForwardAdapter extends RecyclerView.Adapter<PutForwardAdapter.PutForwardViewHolder> {


    private Context context;

    private List<PutForwardTypeEntity> entities;

    private boolean isEdit;

    private OnClickListenter onClickListenter;

    public PutForwardAdapter(Context context, List<PutForwardTypeEntity> entities) {
        this.context = context;
        this.entities = entities;
    }

    public void setOnClickListenter(OnClickListenter onClickListenter) {
        this.onClickListenter = onClickListenter;
    }

    @NonNull
    @Override
    public PutForwardViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new PutForwardViewHolder(LayoutInflater.from(context).inflate(R.layout.put_forward_view_holder, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PutForwardViewHolder putForwardViewHolder, final int i) {

        if(entities.get(i).withdrawType == 0){
            //银行卡
            putForwardViewHolder.imgBank.setImageResource(R.mipmap.pay_bank);
            putForwardViewHolder.tvBank.setText("【" + entities.get(i).bankName + "】" + entities.get(i).username);
            if (isEdit) {
                putForwardViewHolder.tvDelete.setVisibility(View.VISIBLE);
                putForwardViewHolder.imgChoese.setVisibility(View.GONE);
            } else {
                putForwardViewHolder.tvDelete.setVisibility(View.GONE);
                putForwardViewHolder.imgChoese.setVisibility(View.VISIBLE);
            }

            putForwardViewHolder.tvCardId.setText(entities.get(i).cardCode);
        }else{
            //支付宝
            putForwardViewHolder.imgBank.setImageResource(R.mipmap.pay_alipay_new);
            putForwardViewHolder.tvBank.setText(entities.get(i).username);
            if (isEdit) {
                putForwardViewHolder.tvDelete.setVisibility(View.VISIBLE);
                putForwardViewHolder.imgChoese.setVisibility(View.GONE);
            } else {
                putForwardViewHolder.tvDelete.setVisibility(View.GONE);
                putForwardViewHolder.imgChoese.setVisibility(View.VISIBLE);
            }

            putForwardViewHolder.tvCardId.setText(entities.get(i).cardCode);
        }




        if (entities.get(i).isChoese()) {
            putForwardViewHolder.imgChoese.setImageResource(R.mipmap.ic_public_choice_select_main);
        } else {
            putForwardViewHolder.imgChoese.setImageResource(R.mipmap.ic_public_choice_unselect);
        }

        putForwardViewHolder.imgChoese.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenter != null) {
                    onClickListenter.onSelect(i, v);
                }
            }
        });

        putForwardViewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenter != null) {
                    onClickListenter.ondel(i, v);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return entities.size();
    }

    public class PutForwardViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_bank)
        ImageView imgBank;
        @BindView(R.id.tv_bank)
        TextView tvBank;
        @BindView(R.id.img_choese)
        ImageView imgChoese;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_card_id)
        TextView tvCardId;

        public PutForwardViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public interface OnClickListenter {
        void ondel(int i, View view);

        void onSelect(int i, View view);
    }

}
